<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="80px">
    <el-form-item label="新密码" prop="password">
      <el-input
        v-model="user.password"
        placeholder="请输入新密码"
        type="password"
        show-password
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { editUserPassword } from '@/api/system/account'

export default {
  props: {
    userInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      user: {
        password: undefined
      },
      // 表单校验
      rules: {
        password: [
          { required: true, message: '新密码不能为空', trigger: '' }
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const action_id = this.$props.userInfo.admin_id
          editUserPassword({
            ...this.user,
            action_id
          }).then((res) => {
            this.$modal.msgSuccess('修改成功')
            setTimeout(() => {
              this.$store.dispatch('LogOut').then(() => {
                location.href = '/index'
              })
            }, 1500)
          })
        }
      })
    },
    close() {
      this.$tab.closePage()
    }
  }
}
</script>
